What is string-collapse-leading-whitespace?
The string-collapse-leading-whitespace npm package is designed to collapse leading whitespace in strings. This can be particularly useful for cleaning up user input, formatting text, or preparing strings for further processing.
What are string-collapse-leading-whitespace's main functionalities?
Collapse Leading Whitespace
This feature removes leading whitespace from the input string. In the example, the leading spaces before 'Hello World!' are removed.
const collapseLeadingWhitespace = require('string-collapse-leading-whitespace');
const result = collapseLeadingWhitespace(' Hello World!');
console.log(result); // 'Hello World!'
Collapse Leading and Trailing Whitespace
This feature removes both leading and trailing whitespace from the input string. In the example, the spaces before and after 'Hello World!' are removed.
const collapseLeadingWhitespace = require('string-collapse-leading-whitespace');
const result = collapseLeadingWhitespace(' Hello World! ');
console.log(result); // 'Hello World!'
Other packages similar to string-collapse-leading-whitespace
trim
The 'trim' package is a simple utility to remove leading and trailing whitespace from strings. It is similar to string-collapse-leading-whitespace but focuses on trimming both ends of the string without additional customization options.
lodash.trim
The 'lodash.trim' function from the Lodash library removes leading and trailing whitespace or specified characters from a string. It offers more flexibility compared to string-collapse-leading-whitespace, as it allows for trimming specific characters.
validator
The 'validator' package includes a 'trim' function that removes leading and trailing whitespace from strings. It is part of a larger library for string validation and sanitization, providing more comprehensive functionality beyond just whitespace trimming.
string-collapse-leading-whitespace
Collapse the leading and trailing whitespace of a string
Install
This package is pure ESM. If you're not ready yet, install an older version of this program, 5.1.0 (npm i string-collapse-leading-whitespace@5.1.0
).
npm i string-collapse-leading-whitespace
Quick Take
import { strict as assert } from "assert";
import { collWhitespace } from "string-collapse-leading-whitespace";
assert.equal(collWhitespace(" aaa "), " aaa ");
assert.equal(collWhitespace(" \n\n aaa \n\n\n "), "\naaa\n");
assert.equal(collWhitespace("aaa"), "aaa");
assert.equal(
collWhitespace(" abc \n def \n ghi "),
" abc \n def \n ghi "
);
Documentation
Please visit codsen.com for a full description of the API.
Contributing
To report bugs or request features or assistance, raise an issue on GitHub.
Licence
MIT License.
Copyright © 2010-2022 Roy Revelt and other contributors.